#include "BondHeaderView.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <widgets/STips.h>
#include <widgets/table/STable.h>

#include <QMouseEvent>
#include <QPainter>

using namespace qb;

BondHeaderView::BondHeaderView(Qt::Orientation orientation, QWidget* parent)
    : QHeaderView(orientation, parent) {}

void BondHeaderView::paintSection(QPainter* painter, const QRect& rect,
                                  int logicalIndex) const {
  QString tooltip = model()
                        ->headerData(logicalIndex, orientation(), PrefixTipRole)
                        .toString();

  Qt::Alignment align = static_cast<Qt::AlignmentFlag>(
      model()
          ->headerData(logicalIndex, orientation(), Qt::TextAlignmentRole)
          .toInt());

  if (!tooltip.isEmpty()) {
    QRect rc = rect, rcTips = rect;
    painter->fillRect(
        rc, model()
                ->headerData(logicalIndex, orientation(), Qt::BackgroundRole)
                .value<QColor>());
    painter->setPen(
        model()
            ->headerData(logicalIndex, orientation(), BorderColorRole)
            .value<QColor>());
    painter->drawLine(rc.right(), rc.top(), rc.right(), rc.bottom());
    painter->setPen(
        model()
            ->headerData(logicalIndex, orientation(), Qt::ForegroundRole)
            .value<QColor>());

    if ((align & Qt::AlignLeft) == Qt::AlignLeft) {
      rc.adjust(4, 0, -24, 0);
    } else if ((align & Qt::AlignCenter) == Qt::AlignCenter) {
      rc.adjust(24, 0, -24, 0);
    } else if ((align & Qt::AlignRight) == Qt::AlignRight) {
      rc.adjust(4, 0, -24, 0);
    }

    rcTips.setTop(rc.top() + rc.height() / 2 - 8);
    rcTips.setBottom(rcTips.top() + 16);

    QFontMetrics metrics(painter->font());
    QString text =
        model()
            ->headerData(logicalIndex, orientation(), Qt::DisplayRole)
            .toString();
    int textWidth = metrics.width(text);

    if (textWidth > rc.width()) {
      text = metrics.elidedText(text, Qt::ElideRight, rc.width());

      rcTips.setRight(rect.right() - 4);
      rcTips.setLeft(rcTips.right() - 16);
    } else {
      if ((align & Qt::AlignLeft) == Qt::AlignLeft) {
        rcTips.setLeft(rc.left() + textWidth + 4);
        rcTips.setRight(rcTips.left() + 16);
      } else if ((align & Qt::AlignCenter) == Qt::AlignCenter) {
        rcTips.setLeft(rc.left() + textWidth);
        rcTips.setRight(rcTips.left() + 16);
      } else {
        rcTips.setRight(rect.right() - 4);
        rcTips.setLeft(rcTips.right() - 16);
      }
    }

    painter->drawText(rc, align | Qt::TextSingleLine, text);

    SImage* imgTips = ImgMgr::instance().image(
        "widgets_circle_info", WidgetStatus::kNormal, devicePixelRatioF());
    painter->drawImage(QPoint(rcTips.left(), rcTips.top()), *imgTips);

    auto it = m_tips.find(logicalIndex);
    if (it == m_tips.end()) {
      std::pair<QRect, QString> item;
      item.first = rcTips;
      item.second = tooltip;
      m_tips.insert(std::make_pair(logicalIndex, item));
    }
  } else {
    QHeaderView::paintSection(painter, rect, logicalIndex);
  }
}

void BondHeaderView::mouseMoveEvent(QMouseEvent* event) {
  QHeaderView::mouseMoveEvent(event);

  QPoint mousePos = event->pos();

  bool find = false;
  for (auto it = m_tips.begin(); it != m_tips.end(); ++it) {
    if (it->second.first.contains(mousePos)) {
      dealTips(event, it->second.second);
      find = true;
      break;
    }
  }

  if (!find) {
    QBTips::hideTip();
  }
}

void BondHeaderView::mousePressEvent(QMouseEvent* event) {
  QPoint mousePos = event->pos();

  for (auto it = m_tips.begin(); it != m_tips.end(); ++it) {
    if (it->second.first.contains(mousePos)) {
      dealTips(event, it->second.second);
      return;
    }
  }

  QHeaderView::mousePressEvent(event);
}

void BondHeaderView::leaveEvent(QEvent* event) {
  QBTips::hideTip();

  QHeaderView::leaveEvent(event);
}

void BondHeaderView::dealTips(QMouseEvent* event, const QString& strTips) {
  std::vector<QBTipCustomItem> vctCustomItem;
  QBTipCustomItem item;
  item.row = 0;
  item.col = 1;
  item.clrNormal = StyleMgr::instance().color("TextNormal");
  vctCustomItem.push_back(item);
  QBTips::showText(event->screenPos().toPoint(), strTips, this, &vctCustomItem);
}
